import { loadBase64 } from "./util.js";
import { BaseTexture, AnimatedSprite,Texture } from 'pixi.js'

export async function clipAfterimage(resource, afterimage, behavior) {
    const AfterImage = resource.Content.Character.Afterimage[afterimage + '.img'][0][behavior];
    const sit = Number(Object.keys(AfterImage)[0]);
    const length = Object.keys(
        AfterImage[sit]
    ).length;
    const { lt, rb } = AfterImage;
    const Textures = [];
    
    for (let i = 0; i < length; i ++) {
        let image = await loadBase64(AfterImage[sit][i].value);
        let base = new BaseTexture(image);
        let texture = new Texture(base);
        texture.defaultAnchor.x = (AfterImage[sit][i].origin.x) / image.width;
        texture.defaultAnchor.y = (AfterImage[sit][i].origin.y) / image.height;
        Textures.push(texture);
    }
    let animation = new AnimatedSprite(Textures);
    animation.updateAnchor = true;
    animation.zIndex = 10000000;
    return {
        sit,
        ani: animation
    }

}

export async function clipAfterimageHit(resource, weaponType) {
    const AfterImage = resource.Content.Character.Afterimage['hit.img'][weaponType];
    let length = Object.keys(AfterImage).length;
    const Textures = [];
    
    for (let i = 0; i < length; i ++) {
        let image = await loadBase64(AfterImage[i].value);
        let base = new BaseTexture(image);
        let texture = new Texture(base);
        texture.defaultAnchor.x = (AfterImage[i].origin.x) / image.width;
        texture.defaultAnchor.y = (AfterImage[i].origin.y) / image.height;
        Textures.push(texture);
    }
    let animation = new AnimatedSprite(Textures);
    animation.updateAnchor = true;
    animation.zIndex = 10000000;
    return animation;
}